/*=============================================================
(c) all rights reserved

GraydonDataServices examples

================================================================*/
using System;
using System.Web.Services.Protocols;

/**
 * @author denisreilly
 *
 * Runnable .net 2.0 client for Graydon Company Data Webservice
 * 
 * Operation : checkOrders
 * 
 * Argument0 = Partner Userid
 * Argument1 = Partner Password
 * Argument2 = Order State
 */
class CheckOrders{   
   public static void Main(string[] args)
   {
      
       try
	   {
	      string partnerUserid             = null;
	      string partnerPassword	       = null;
	      string state                     = null;
	      
	      // retrieve arguments
	      if (args.Length == 0)
			throw new Exception("Invalid arguments: Please specify match arguments");
			
	      // populate match parameters with arguments
	      partnerUserid            = args[0].Trim();
	      partnerPassword          = args[1].Trim();
	      state                    = args[2].Trim();
	            
	      GraydonCompanyData graydonCompanyData = new GraydonCompanyData();
	      
	      // create the authentication parameters
	      Authentication_ParametersType authentication_Parameters                                    = new Authentication_ParametersType();
	      authentication_Parameters.PartnerUserId                                                    = partnerUserid;
	      authentication_Parameters.PartnerPassword                                                  = partnerPassword;
	      
	      // create the parameters
	      CheckOrders_ParametersType checkOrders_Parameters                                          = new CheckOrders_ParametersType();
	      checkOrders_Parameters.Authentication_Parameters                                           = authentication_Parameters;
	      OrderStateType orderState                                                                  = new OrderStateType();
	      if (state.Equals("Completed"))
	         orderState = OrderStateType.Completed;
	      else if (state.Equals("Assigned"))
	         orderState = OrderStateType.Assigned;
	      else if (state.Equals("InError"))
	         orderState = OrderStateType.InError;
	      else if (state.Equals("Initialised"))
	         orderState = OrderStateType.Initialised;
	      else if (state.Equals("Delivered"))
	         orderState = OrderStateType.Delivered;
	      else if (state.Equals("Notified"))
	         orderState = OrderStateType.Notified;
	      else if (state.Equals("ReCompleted"))
	         orderState = OrderStateType.ReCompleted;	         	         
	      checkOrders_Parameters.OrderState                                                          = orderState;

	      // Begin the Async call to "checkOrders"
	      IAsyncResult ar = graydonCompanyData.BegincheckOrders(checkOrders_Parameters, null, graydonCompanyData);
	      
	      // wait for response
	      ar.AsyncWaitHandle.WaitOne();
	      
	      CheckOrders_ResultType checkOrders_Result = null;
	      
	      try
          {
             // get the results  
             checkOrders_Result = graydonCompanyData.EndcheckOrders(ar);
          }
          catch (SoapException soapE)
          {
            Console.WriteLine(" ");
            // SOAP 1.1 Fault info
            Console.WriteLine("SOAP 1.1 fault...");
            Console.WriteLine("Code:/t/t" + soapE.Code.ToString());
            Console.WriteLine("Actor:/t/t" + soapE.Actor);
            Console.WriteLine("Detail:/t/t" + soapE.Detail.InnerXml);
            throw new Exception("Fault occurred whilst accessing the webservice.");
          }
          
         // get the service log details
         serviceLog(checkOrders_Result.Service_Log);
               
 	     // check if an order status has been returned
		 if (checkOrders_Result.CheckOrders != null)
	     {	
			// get the order status detail
			OrderStatusType[] orderStatus = checkOrders_Result.CheckOrders;
			
			for (int x = 0; x < orderStatus.Length; x++)
			{
				Console.WriteLine("Order Status:");
				Console.WriteLine("  Order Reference\t: " + orderStatus[x].OrderReference);	         						// output company name
				Console.WriteLine("  Order State\t\t: " + orderStatus[x].OrderState);				      					// output order state
				Console.WriteLine("  Date Ordered\t\t: " + "{0:d}",orderStatus[x].DateOrdered);			                    // output date ordered
				Console.WriteLine("  Date Completed\t: " + "{0:d}",orderStatus[x].DateCompleted);			                // output date completed
				
				// get some order details
				if (orderStatus[x].Order != null)
				{
					OrderType order = orderStatus[x].Order;
					Console.WriteLine("Order Detail:");			
					Console.WriteLine("  Company Name\t\t: " + order.Name);											        // output company name
					if (order.CompanyIdentifier != null)
						Console.WriteLine("  Company Identifier\t: " + order.CompanyIdentifier.Identifier);		            // output company identifier
				}
				Console.WriteLine(" ");
			}
		 }
	  }  
      catch (Exception e)
      {
	     Console.Write("Exception : " + e);
      }

   }
   
   	/**
	* Service log output
	*
	*/
	private static void serviceLog(Service_LogType service_Log)
	{
		Console.WriteLine("Service_Log :");																	
		Console.WriteLine("  PartnerUserId\t\t: " + service_Log.PartnerUserId);												    // output partner userid
		Console.WriteLine("  SessionId\t\t: " + service_Log.SessionID);														    // output sessionId
		Console.WriteLine("  Transaction Identifier: " + service_Log.TransactionIdentifier);									// output transaction identifier
		Console.WriteLine("  Request Date\t\t: " + "{0:d}", service_Log.RequestTimestamp.Date);				                    // output request date
		Console.WriteLine("  Request Time\t\t: " + "{0:t}", service_Log.RequestTimestamp.Time);							        // output request time
		Console.WriteLine("  Response Date\t\t: " + "{0:d}", service_Log.ResponseTimestamp.Date);				                // output response date
		Console.WriteLine("  Response Time\t\t: " + "{0:t}", service_Log.ResponseTimestamp.Time);								// output response time
		Console.WriteLine(" ");
	}
   
}